import Vue from 'vue'
import Vuex from 'vuex'

 const postUrlPrefix="/api";


 const toast=(object)=>{
	uni.showToast(object)
}
 const error=(object)=>{
	if(object==undefined||object==null || object.message==undefined){
		uni.showToast({
			title:"未知错误",
			icon:'error'
		})
	}else{
		uni.showToast({
			title:object.message,
			icon:'error'
		})
	}
}
 const success=(object)=>{
	uni.showToast({
		title:object.message,
		icon:'success'
	})
}
 
 const get=(url,params,func)=>{
	 uni.request({
	 	url:postUrlPrefix+url,
		data:params,
		method:"GET",
		success:(res)=>{
			if(res.data.status!==200){
				res.result=false;
				error({message:res.data.msg})
				return res;
			}else{
				res.result=true;
				func(res);
				success({message: res.data.msg})
				return res;
			}
		},
		header: {
		    'Content-Type': 'application/x-www-form-urlencoded'
		},
		fail:(res)=>{
			res.result=false;
			error({message: res.data.msg})
			return res;
		}
	 })
 }

 const post=(url,params,func)=>{
	uni.request({
		url:postUrlPrefix+url,
		data:params,
		method:"POST",
		success:(res)=>{
			if(res.data.status!==200){
				res.result=false;
				error({message:res.data.msg})
				return res;
			}else{
				res.result=true;
				func(res);
				success({message: res.data.msg})
				return res;
			}
			
		},
		header: {
		    'Content-Type': 'application/x-www-form-urlencoded'
		},
		fail:(res)=>{
			res.result=false;
			error({message: res.data.msg})
			return res;
		}
	})
}

Vue.use(Vuex);
Vue.prototype.post=post;
Vue.prototype.post=get;
const store = new Vuex.Store({
    state: {
		sendedCode:false,
		count:60,
		user:{
			name:"张生",
			sex:1,
			
		},
		interval:null,
		loading:false
	},
    mutations: {
		setSendedCode(state,sendedCode){
			state.sendedCode=sendedCode;
		},
		setCount(state,count){
			state.count=count;
			uni.setStorageSync('countStorage',count);
		},
		getCount(state){
			return state.count;
		},
		setStateInterval(state,interval){
			state.interval=interval;
			uni.setStorageSync('intervalStorage',interval);
		},
		getSendedCode(state){
			return state.sendedCode;
		},
		cleanInterval(state){
			clearInterval(state.interval);
			uni.setStorageSync("intervalStorage",null);
			uni.setStorageSync("countStorage",null);
			state.interval=null;
		},
		setLoading(state,loading){
			state.loading=loading;
			
		},
		getLoaing(state){
			return state.loading;
		},
		setUser(state,user){
			state.user=user;
		}
	},
	actions:{
		sendCode(context,phone){
			uni.showLoading({
				title:"正在发送验证码",
				mask:true
			})
			post('/verify-code',{"type":'phoneCode',"phone":phone},(res)=>{
				uni.hideLoading();
				if(res.result==true){
					context.commit('setSendedCode',true);
					let inter=setInterval(()=>{
						if(context.state.count>0){
							context.commit('setCount',context.state.count-1);
						}else{
							context.commit('setCount',60);
							context.commit('setSendedCode',false);
							context.commit('cleanInterval');
						}
					},1000);
					context.commit('setStateInterval',inter);
				}
			});
			
		},
		getInterval(context){
			if(context.state.interval==null){
				let inter=uni.getStorageSync('intervalStorage');
				if(inter!==null){
					clearInterval(inter);
					context.commit('setCount',uni.getStorageSync("countStorage")+0);
					context.state.interval=inter;
					context.state.sendedCode=true;
					let inter=setInterval(()=>{
						if(context.state.count>0){
							context.commit('setCount',context.state.count-1);
						}else{
							context.commit('setCount',60);
							context.commit('setSendedCode',false);
							context.commit('cleanInterval');
						}
					},1000);
					context.commit('setStateInterval',inter);
				}
			}
		}
	}
})
export default store